function axios(options={}){
	let {url,type="get",data,params={}} = options;
	return new Promise(function (resolve,reject){
		// {a:1,b:2} ===> a=1&b=2
		console.log(Object.keys(params));// [a,b]
		console.log(Object.keys(params).map(key=>key+"="+params[key]));// [a=1,b=2]
		console.log(Object.keys(params).map(key=>key+"="+params[key]).join("&"));// [a=1,b=2]
		
		const paramsStr = Object.keys(params).map(key=>key+"="+params[key]).join("&");
		if(paramsStr.length>0) url += "?"+paramsStr;
		
		
		
		const xhr = new XMLHttpRequest();
		xhr.responseType = "json";
		xhr.open(type,url);
		if(type === "post"){
			if(typeof data === "object"){
				xhr.setRequestHeader("Content-Type","application/json")
				xhr.send(JSON.stringify(data));
			}else if(typeof data === "string"){
				xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xhr.send(data);
			}
			
		}
		else xhr.send();
		xhr.onload = function(){
			resolve({
				data:xhr.response
			});
		}
	})
}
axios.get = function(url,options){
	return this({
		url,
		...options
	})
}
axios.post = function(url,data={},options={}){
	return this({
		url,
		type:"post",
		data,
		...options
	})
}

export default axios;